package com.xiaolun.controller;

import com.xiaolun.mapper.UserMapper;
import com.xiaolun.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author 硕鼠
 * @Date 2020/4/10 21:04
 * @Version 1.0
 * @Description 文件说明
 */
@RestController
public class UserController {

    @Autowired
    UserMapper userMapper;

    //查询所有用户（通过测试发现结果为json数据格式）
    @RequestMapping("/findAllList") //该注解和@GetMapping注解同，区别在哪
    public List<User> findAllList(){
        List<User> userList = this.userMapper.findAll();

//        for (User userTemp:userList) {
//            System.out.println(userList);
//        }
        System.out.println("是否为json数据：");
        System.out.println(userList);
        return userList;
    }

    //根据id查找用户
    @GetMapping("/queryUserById")
    public String  queryUserById(){
        User user = this.userMapper.queryUserById(1);
        System.out.println(user.getAuthor());
        return user.toString();
    }

    //添加用户
    @GetMapping("/addUser")
    public List<User>  addUser(){
//        User user = new User();
//        user.setTitle("x");
//        user.setAuthor("1");
//        user.setNote("11");
        int result = this.userMapper.addUser(new User(2,"期刊","xiaoming","很好"));
//        int result = this.userMapper.addUser(user);
        List<User> userList = this.userMapper.findAll();
        for (User userTemp:userList) {
            System.out.println(userList);
        }
        return userList;
    }

    //修改一个用户
    @GetMapping("/updateUser")
    public List<User>  updateUser(){
//        User user = new User();
//        user.setTitle("x");
//        user.setAuthor("1");
//        user.setNote("11");

        int result = this.userMapper.updateUser(new User(2,"期刊","111","很好"));
//        int result = this.userMapper.updateUser(user);
        List<User> userList = this.userMapper.findAll();
        for (User userTemp:userList) {
            System.out.println(userList);
        }
        return userList;
    }
    //删除一个用户
    @GetMapping("/deleteUser")
    public List<User>  deleteUser(){
        int result = this.userMapper.deleteUser(1);
        System.out.println("数据长度"+result);
        List<User> userList = this.userMapper.findAll();
        for (User userTemp:userList) {
            System.out.println(userList);
        }
        return userList;
    }
}
